#include "Condition.h"

namespace BASE{
	Condition::Condition(){
		int ret = pthread_cond_init(&cond, NULL);
		assert(ret == 0);
	}

	Condition::~Condition(){
		pthread_cond_destroy(&cond);
	}

	void Condition::notify(){
		pthread_cond_signal(&cond);
	}

	void Condition::notifyAll(){
		pthread_cond_broadcast(&cond);
	}

	void Condition::wait(Mutex& mutex){
		pthread_cond_wait(&cond, mutex.getMutex());
	}

	void Condition::wait(Mutex& mutex, int millsec){ 
		struct timespec abstime;
		clock_gettime(CLOCK_REALTIME, &abstime);
		abstime.tv_nsec += millsec * 1000000;
		pthread_cond_timedwait(&cond, mutex.getMutex(), &abstime);
	}
}
